package com.yanyilu.basing.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yanyilu.basing.common.R;
import com.yanyilu.basing.entity.Activity;
import com.yanyilu.basing.entity.TotalTimeMileage;
import com.yanyilu.basing.entity.User;
import com.yanyilu.basing.entity.dto.UserDTO;
import com.yanyilu.basing.jwt.Audience;
import com.yanyilu.basing.mapper.*;
import com.yanyilu.basing.service.ILeaderboardService;
import com.yanyilu.basing.service.ITotalTimeMileageService;
import com.yanyilu.basing.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yanyilu.basing.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Sxx
 * @since 2020-10-26
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private TotalTimeMileageMapper totalTimeMileageMapper;

    @Autowired
    ActivityMapper activityMapper;

    @Autowired
    Audience audience;

    @Autowired
    OpenidUserMapper openidUserMapper;

    @Autowired
    ILeaderboardService leaderboardService;

    @Autowired
    OrderFormMapper orderFormMapper;

    /**
     * 保存
     * @param user
     * @return
     */
    @Override
    @Transactional
    public R addUser(User user) {

        //TODO
        user.setIsUnbox(0);
        user.setIsPay(0);
        user.setStatus(1);
        user.setIsReceive(0);
        user.setUserName(user.getRealName());

        int time = TimeUtil.getTime();
        Activity activity = activityMapper.selectById(1);
        Integer startTime = activity.getSignUpStartTime();
        Integer endTime = activity.getSignUpEndTime();
        if(time >= startTime && time <= endTime){
            user.setSignUpState(1);
            userMapper.updateById(user);
        }else{
            user.setSignUpState(0);
            userMapper.updateById(user);
            return R.error("当前活动已结束！");
        }


        //保存公里数
        User user1 = userMapper.selectById(user.getUserId());

        TotalTimeMileage totalTimeMileage = totalTimeMileageMapper.selectByUserId(user.getUserId());
        if(null == totalTimeMileage) {
            totalTimeMileage = new TotalTimeMileage();
            totalTimeMileage.setUserId(user1.getUserId());
            totalTimeMileage.setTotalMileage(0);
            totalTimeMileage.setTotalTime(0);
            totalTimeMileage.setUserName(user1.getRealName());
            totalTimeMileage.setHeadImage(user1.getHeadImge());
            totalTimeMileageMapper.insert(totalTimeMileage);
        } else{
            totalTimeMileage.setUserId(user1.getUserId());
            totalTimeMileage.setUserName(user1.getRealName());
            totalTimeMileage.setHeadImage(user1.getHeadImge());
            totalTimeMileageMapper.updateById(totalTimeMileage);
        }


        return R.ok().put("user",user1);
    }

    /**
     * 更新
     * @param user
     * @return
     */
    @Override
    public R updateUser(User user) {
        userMapper.updateById(user);
        return R.ok();
    }


    @Override
    public R AdminDeleteUser(Integer userId) {
        userMapper.deleteById(userId);
        openidUserMapper.deleteByUserId(userId);
        leaderboardService.adminDeleteByUserId(userId);
        orderFormMapper.deleteByUserId(userId);
        return R.ok();
    }

    @Override
    public R selectByAdminList(Integer page,Integer limit,String userName,String contactDetails){
        PageHelper.startPage(page,limit);
        List<User> list = userMapper.selectByAdminList(userName,contactDetails);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return R.ok().put("list",pageInfo.getList()).put("count",pageInfo.getTotal()).put("pages",pageInfo.getPages());
    }

    @Override
    public R countNum() {
        Integer count = userMapper.countPeople();
        return R.ok().put("count",count);
    }
}
